﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Speakup;
using System.Windows.Threading;
using TableTypes;

namespace SpeakupUI
{
    class ClassPanelList
    {
        private List<ClassPanel> m_panels = new List<ClassPanel>();
        private DispatcherTimer HighlightTimer = null;
        
        public static ClassPanelList FromClassList(List<Class> classes)
        {
            ClassPanelList list = new ClassPanelList();

            foreach (Class entry in classes)
            {
                list.Add(new ClassPanel(entry));
            }

            return list;
        }

        public bool m_useAutoFilter = true;
        public bool UseAutoFilter
        {
            get
            {
                return m_useAutoFilter;
            }
            set
            {
                m_useAutoFilter = value;
                ApplyFilter();
            }
        }

        private Dictionary<ClassType, bool> ManualFilter = new Dictionary<ClassType, bool>();

        public void Add(ClassPanel classPanel)
        {
            classPanel.HighlightEvent += PanelHighlighted;
            classPanel.SelectionEvent += PanelSelected;
            m_panels.Add(classPanel);
        }
        public void Remove(ClassPanel classPanel)
        {
            classPanel.HighlightEvent -= PanelHighlighted;
            classPanel.SelectionEvent -= PanelSelected;
            m_panels.Remove(classPanel);
        }

        public void SetFilter(ClassType type, bool show)
        {
            ManualFilter[type] = show;

            if (UseAutoFilter == false)
            {
                ApplyFilter();
            }
        }
        public bool GetFilter(ClassType type)
        {
            if (ManualFilter.Keys.Contains(type))
            {
                return ManualFilter[type];
            }
            return true;
        }

        void ApplyFilter()
        {
            if (m_useAutoFilter)
            {
                foreach (ClassPanel classPanel in m_panels)
                {
                    classPanel.IsEnabled = true;
                    classPanel.FadedOut = false;
                }
            }
            else
            {
                foreach (ClassPanel classPanel in m_panels)
                {
                    bool fadeOut = !GetFilter(classPanel.SpeakupClass.ClassName.Type);
                    classPanel.FadedOut = fadeOut;
                    classPanel.IsEnabled = !fadeOut;
                }
            }
        }

        public ICollection<ClassPanel> Panels
        {
            get
            {
                return m_panels;
            }
        }
        public ClassPanel SelectedPanel { get; private set; }
        
        void PanelHighlighted(object sender, EventArgs e)
        {
            if (HighlightEvent != null)
            {
                HighlightEvent(sender, e);
            }

            if (UseAutoFilter == false) return;

            ClassPanel panel = (ClassPanel)sender;

            if (HighlightTimer != null)
            {
                HighlightTimer.Stop();
            }
            else
            {
                HighlightTimer = new DispatcherTimer();
                HighlightTimer.Tick += OnFadeOutTick;
            }

            HighlightTimer.Tag = panel;

            if (panel.Highlight)
            {
                HighlightTimer.Interval = new TimeSpan(0, 0, 0, 0, 300);
            }
            else
            {
                HighlightTimer.Interval = new TimeSpan(0, 0, 0, 0, 50);
            }

            HighlightTimer.Start();             
            
        }

        void OnFadeOutTick(object sender, EventArgs e)
        {
            if (m_useAutoFilter == false) return;

            ClassPanel panel = (ClassPanel)HighlightTimer.Tag;

            if (panel.Highlight)
            {
                foreach (ClassPanel entry in m_panels)
                {
                    if (entry.SpeakupClass.ClassName.Type !=
                        panel.SpeakupClass.ClassName.Type)
                    {
                        entry.FadedOut = true;
                    }
                    else
                    {
                        entry.FadedOut = false;
                    }
                }
            }
            else
            {
                foreach (ClassPanel entry in m_panels)
                {
                    entry.FadedOut = false;
                }
            }
            


            HighlightTimer.Stop();
        }
        void PanelSelected(object sender, EventArgs e)
        {
            if (SelectEvent != null)
            {
                SelectEvent(sender, e);
            }

            ClassPanel panel = (ClassPanel)sender;

            if (panel.Selected == true)
            {
                if (SelectedPanel != null &&
                    SelectedPanel != panel)
                {
                    SelectedPanel.Selected = false;
                }

                SelectedPanel = panel;
            }
            else
            {
                SelectedPanel = null;
            }

            
        }

        public event EventHandler<EventArgs> HighlightEvent;
        public event EventHandler<EventArgs> SelectEvent;
        
    }
}
